/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.stakeholder;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author WenYong
 */
@ManagedBean
@RequestScoped
public class StakeholderAddUnstructuredContentUploadVideoManager {

    private String videoUrl;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private String nextPage;
    private Long stakeholderId;

    /** Creates a new instance of StakeholderAddUnstructuredContentUploadVideoManager */
    public StakeholderAddUnstructuredContentUploadVideoManager() throws KeyStoreException {
        KeyStore.getInstance("JKS");
        ctx.getExternalContext().getFlash().clear();
        //    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        stakeholderId = (Long) ctx.getExternalContext().getSessionMap().get("stakeholderId");

        if (stakeholderId == null) {

            nextPage = (ctx.getExternalContext().encodeActionURL(FacesContext.getCurrentInstance().getApplication().getViewHandler().getActionURL(FacesContext.getCurrentInstance(), "/StakeholderLogin.xhtml")));
            try {
                ctx.getExternalContext().redirect(nextPage);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    public void confirmVideo() {
        ctx.getExternalContext().getSessionMap().put("videoName", videoUrl);
        String url = "";
        if (ctx.getExternalContext().getSessionMap().containsKey("returnPage")) {
            url = ctx.getExternalContext().getSessionMap().get("returnPage").toString();
        }
        try {
            String serverName = ctx.getExternalContext().getRequestServerName();
            int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/" + url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void videoAdd() {
        String domain = "http://www.youtube.com/v/";
        int posOfAnd = videoUrl.indexOf("&");
        int equal = videoUrl.indexOf("=");
        equal = equal + 1;
        videoUrl = videoUrl.substring(equal, posOfAnd);
        videoUrl = domain + videoUrl;
    }
}
